﻿Imports System
Imports System.Collections.Generic
Imports System.Text
Imports Microsoft.DirectX


	Public Class ClassMCube

    '   Converted from C code ref. http://local.wasp.uwa.edu.au/~pbourke/geometry/polygonise/
    '   Polygonising a scalar field
    '   Also known as: "3D Contouring", "Marching Cubes", "Surface Reconstruction" 
    '   by Paul Bourke (http://local.wasp.uwa.edu.au/~pbourke/geometry/)
    '   May 1994

    '   Given a grid cell and an isolevel, calculate the triangular
    '   facets required to represent the isosurface through the cell.
    '   Return the number of triangular facets, the array "triangles"
    '   will be loaded up with the vertices at most 5 triangular facets.
    '	0 will be returned if the grid cell is either totally above
    '   of totally below the isolevel.
    '

		Public Function Polygonise(grid As ClassMTeth.GRIDCELL, isolevel As Double, ByRef triangles As ClassMTeth.TRIANGLE()) As Integer
			Dim i As Integer, ntriang As Integer
			Dim cubeindex As Integer
			Dim vertlist As Vector3() = New Vector3(11) {}

			Dim edgeTable As Integer() = {&H0, &H109, &H203, &H30a, &H406, &H50f, _
				&H605, &H70c, &H80c, &H905, &Ha0f, &Hb06, _
				&Hc0a, &Hd03, &He09, &Hf00, &H190, &H99, _
				&H393, &H29a, &H596, &H49f, &H795, &H69c, _
				&H99c, &H895, &Hb9f, &Ha96, &Hd9a, &Hc93, _
				&Hf99, &He90, &H230, &H339, &H33, &H13a, _
				&H636, &H73f, &H435, &H53c, &Ha3c, &Hb35, _
				&H83f, &H936, &He3a, &Hf33, &Hc39, &Hd30, _
				&H3a0, &H2a9, &H1a3, &Haa, &H7a6, &H6af, _
				&H5a5, &H4ac, &Hbac, &Haa5, &H9af, &H8a6, _
				&Hfaa, &Hea3, &Hda9, &Hca0, &H460, &H569, _
				&H663, &H76a, &H66, &H16f, &H265, &H36c, _
				&Hc6c, &Hd65, &He6f, &Hf66, &H86a, &H963, _
				&Ha69, &Hb60, &H5f0, &H4f9, &H7f3, &H6fa, _
				&H1f6, &Hff, &H3f5, &H2fc, &Hdfc, &Hcf5, _
				&Hfff, &Hef6, &H9fa, &H8f3, &Hbf9, &Haf0, _
				&H650, &H759, &H453, &H55a, &H256, &H35f, _
				&H55, &H15c, &He5c, &Hf55, &Hc5f, &Hd56, _
				&Ha5a, &Hb53, &H859, &H950, &H7c0, &H6c9, _
				&H5c3, &H4ca, &H3c6, &H2cf, &H1c5, &Hcc, _
				&Hfcc, &Hec5, &Hdcf, &Hcc6, &Hbca, &Hac3, _
				&H9c9, &H8c0, &H8c0, &H9c9, &Hac3, &Hbca, _
				&Hcc6, &Hdcf, &Hec5, &Hfcc, &Hcc, &H1c5, _
				&H2cf, &H3c6, &H4ca, &H5c3, &H6c9, &H7c0, _
				&H950, &H859, &Hb53, &Ha5a, &Hd56, &Hc5f, _
				&Hf55, &He5c, &H15c, &H55, &H35f, &H256, _
				&H55a, &H453, &H759, &H650, &Haf0, &Hbf9, _
				&H8f3, &H9fa, &Hef6, &Hfff, &Hcf5, &Hdfc, _
				&H2fc, &H3f5, &Hff, &H1f6, &H6fa, &H7f3, _
				&H4f9, &H5f0, &Hb60, &Ha69, &H963, &H86a, _
				&Hf66, &He6f, &Hd65, &Hc6c, &H36c, &H265, _
				&H16f, &H66, &H76a, &H663, &H569, &H460, _
				&Hca0, &Hda9, &Hea3, &Hfaa, &H8a6, &H9af, _
				&Haa5, &Hbac, &H4ac, &H5a5, &H6af, &H7a6, _
				&Haa, &H1a3, &H2a9, &H3a0, &Hd30, &Hc39, _
				&Hf33, &He3a, &H936, &H83f, &Hb35, &Ha3c, _
				&H53c, &H435, &H73f, &H636, &H13a, &H33, _
				&H339, &H230, &He90, &Hf99, &Hc93, &Hd9a, _
				&Ha96, &Hb9f, &H895, &H99c, &H69c, &H795, _
				&H49f, &H596, &H29a, &H393, &H99, &H190, _
				&Hf00, &He09, &Hd03, &Hc0a, &Hb06, &Ha0f, _
				&H905, &H80c, &H70c, &H605, &H50f, &H406, _
				&H30a, &H203, &H109, &H0}
			Dim triTable As Integer(,) = {{-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {0, 8, 3, -1, -1, -1, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {0, 1, 9, -1, -1, -1, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {1, 8, 3, 9, 8, 1, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {1, 2, 10, -1, -1, -1, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {0, 8, 3, 1, 2, 10, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, _
				{9, 2, 10, 0, 2, 9, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {2, 8, 3, 2, 10, 8, _
				10, 9, 8, -1, -1, -1, _
				-1, -1, -1, -1}, {3, 11, 2, -1, -1, -1, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {0, 11, 2, 8, 11, 0, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {1, 9, 0, 2, 3, 11, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {1, 11, 2, 1, 9, 11, _
				9, 8, 11, -1, -1, -1, _
				-1, -1, -1, -1}, _
				{3, 10, 1, 11, 10, 3, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {0, 10, 1, 0, 8, 10, _
				8, 11, 10, -1, -1, -1, _
				-1, -1, -1, -1}, {3, 9, 0, 3, 11, 9, _
				11, 10, 9, -1, -1, -1, _
				-1, -1, -1, -1}, {9, 8, 10, 10, 8, 11, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {4, 7, 8, -1, -1, -1, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {4, 3, 0, 7, 3, 4, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, _
				{0, 1, 9, 8, 4, 7, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {4, 1, 9, 4, 7, 1, _
				7, 3, 1, -1, -1, -1, _
				-1, -1, -1, -1}, {1, 2, 10, 8, 4, 7, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {3, 4, 7, 3, 0, 4, _
				1, 2, 10, -1, -1, -1, _
				-1, -1, -1, -1}, {9, 2, 10, 9, 0, 2, _
				8, 4, 7, -1, -1, -1, _
				-1, -1, -1, -1}, {2, 10, 9, 2, 9, 7, _
				2, 7, 3, 7, 9, 4, _
				-1, -1, -1, -1}, _
				{8, 4, 7, 3, 11, 2, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {11, 4, 7, 11, 2, 4, _
				2, 0, 4, -1, -1, -1, _
				-1, -1, -1, -1}, {9, 0, 1, 8, 4, 7, _
				2, 3, 11, -1, -1, -1, _
				-1, -1, -1, -1}, {4, 7, 11, 9, 4, 11, _
				9, 11, 2, 9, 2, 1, _
				-1, -1, -1, -1}, {3, 10, 1, 3, 11, 10, _
				7, 8, 4, -1, -1, -1, _
				-1, -1, -1, -1}, {1, 11, 10, 1, 4, 11, _
				1, 0, 4, 7, 11, 4, _
				-1, -1, -1, -1}, _
				{4, 7, 8, 9, 0, 11, _
				9, 11, 10, 11, 0, 3, _
				-1, -1, -1, -1}, {4, 7, 11, 4, 11, 9, _
				9, 11, 10, -1, -1, -1, _
				-1, -1, -1, -1}, {9, 5, 4, -1, -1, -1, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {9, 5, 4, 0, 8, 3, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {0, 5, 4, 1, 5, 0, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {8, 5, 4, 8, 3, 5, _
				3, 1, 5, -1, -1, -1, _
				-1, -1, -1, -1}, _
				{1, 2, 10, 9, 5, 4, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {3, 0, 8, 1, 2, 10, _
				4, 9, 5, -1, -1, -1, _
				-1, -1, -1, -1}, {5, 2, 10, 5, 4, 2, _
				4, 0, 2, -1, -1, -1, _
				-1, -1, -1, -1}, {2, 10, 5, 3, 2, 5, _
				3, 5, 4, 3, 4, 8, _
				-1, -1, -1, -1}, {9, 5, 4, 2, 3, 11, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {0, 11, 2, 0, 8, 11, _
				4, 9, 5, -1, -1, -1, _
				-1, -1, -1, -1}, _
				{0, 5, 4, 0, 1, 5, _
				2, 3, 11, -1, -1, -1, _
				-1, -1, -1, -1}, {2, 1, 5, 2, 5, 8, _
				2, 8, 11, 4, 8, 5, _
				-1, -1, -1, -1}, {10, 3, 11, 10, 1, 3, _
				9, 5, 4, -1, -1, -1, _
				-1, -1, -1, -1}, {4, 9, 5, 0, 8, 1, _
				8, 10, 1, 8, 11, 10, _
				-1, -1, -1, -1}, {5, 4, 0, 5, 0, 11, _
				5, 11, 10, 11, 0, 3, _
				-1, -1, -1, -1}, {5, 4, 8, 5, 8, 10, _
				10, 8, 11, -1, -1, -1, _
				-1, -1, -1, -1}, _
				{9, 7, 8, 5, 7, 9, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {9, 3, 0, 9, 5, 3, _
				5, 7, 3, -1, -1, -1, _
				-1, -1, -1, -1}, {0, 7, 8, 0, 1, 7, _
				1, 5, 7, -1, -1, -1, _
				-1, -1, -1, -1}, {1, 5, 3, 3, 5, 7, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {9, 7, 8, 9, 5, 7, _
				10, 1, 2, -1, -1, -1, _
				-1, -1, -1, -1}, {10, 1, 2, 9, 5, 0, _
				5, 3, 0, 5, 7, 3, _
				-1, -1, -1, -1}, _
				{8, 0, 2, 8, 2, 5, _
				8, 5, 7, 10, 5, 2, _
				-1, -1, -1, -1}, {2, 10, 5, 2, 5, 3, _
				3, 5, 7, -1, -1, -1, _
				-1, -1, -1, -1}, {7, 9, 5, 7, 8, 9, _
				3, 11, 2, -1, -1, -1, _
				-1, -1, -1, -1}, {9, 5, 7, 9, 7, 2, _
				9, 2, 0, 2, 7, 11, _
				-1, -1, -1, -1}, {2, 3, 11, 0, 1, 8, _
				1, 7, 8, 1, 5, 7, _
				-1, -1, -1, -1}, {11, 2, 1, 11, 1, 7, _
				7, 1, 5, -1, -1, -1, _
				-1, -1, -1, -1}, _
				{9, 5, 8, 8, 5, 7, _
				10, 1, 3, 10, 3, 11, _
				-1, -1, -1, -1}, {5, 7, 0, 5, 0, 9, _
				7, 11, 0, 1, 0, 10, _
				11, 10, 0, -1}, {11, 10, 0, 11, 0, 3, _
				10, 5, 0, 8, 0, 7, _
				5, 7, 0, -1}, {11, 10, 5, 7, 11, 5, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {10, 6, 5, -1, -1, -1, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {0, 8, 3, 5, 10, 6, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, _
				{9, 0, 1, 5, 10, 6, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {1, 8, 3, 1, 9, 8, _
				5, 10, 6, -1, -1, -1, _
				-1, -1, -1, -1}, {1, 6, 5, 2, 6, 1, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {1, 6, 5, 1, 2, 6, _
				3, 0, 8, -1, -1, -1, _
				-1, -1, -1, -1}, {9, 6, 5, 9, 0, 6, _
				0, 2, 6, -1, -1, -1, _
				-1, -1, -1, -1}, {5, 9, 8, 5, 8, 2, _
				5, 2, 6, 3, 2, 8, _
				-1, -1, -1, -1}, _
				{2, 3, 11, 10, 6, 5, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {11, 0, 8, 11, 2, 0, _
				10, 6, 5, -1, -1, -1, _
				-1, -1, -1, -1}, {0, 1, 9, 2, 3, 11, _
				5, 10, 6, -1, -1, -1, _
				-1, -1, -1, -1}, {5, 10, 6, 1, 9, 2, _
				9, 11, 2, 9, 8, 11, _
				-1, -1, -1, -1}, {6, 3, 11, 6, 5, 3, _
				5, 1, 3, -1, -1, -1, _
				-1, -1, -1, -1}, {0, 8, 11, 0, 11, 5, _
				0, 5, 1, 5, 11, 6, _
				-1, -1, -1, -1}, _
				{3, 11, 6, 0, 3, 6, _
				0, 6, 5, 0, 5, 9, _
				-1, -1, -1, -1}, {6, 5, 9, 6, 9, 11, _
				11, 9, 8, -1, -1, -1, _
				-1, -1, -1, -1}, {5, 10, 6, 4, 7, 8, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {4, 3, 0, 4, 7, 3, _
				6, 5, 10, -1, -1, -1, _
				-1, -1, -1, -1}, {1, 9, 0, 5, 10, 6, _
				8, 4, 7, -1, -1, -1, _
				-1, -1, -1, -1}, {10, 6, 5, 1, 9, 7, _
				1, 7, 3, 7, 9, 4, _
				-1, -1, -1, -1}, _
				{6, 1, 2, 6, 5, 1, _
				4, 7, 8, -1, -1, -1, _
				-1, -1, -1, -1}, {1, 2, 5, 5, 2, 6, _
				3, 0, 4, 3, 4, 7, _
				-1, -1, -1, -1}, {8, 4, 7, 9, 0, 5, _
				0, 6, 5, 0, 2, 6, _
				-1, -1, -1, -1}, {7, 3, 9, 7, 9, 4, _
				3, 2, 9, 5, 9, 6, _
				2, 6, 9, -1}, {3, 11, 2, 7, 8, 4, _
				10, 6, 5, -1, -1, -1, _
				-1, -1, -1, -1}, {5, 10, 6, 4, 7, 2, _
				4, 2, 0, 2, 7, 11, _
				-1, -1, -1, -1}, _
				{0, 1, 9, 4, 7, 8, _
				2, 3, 11, 5, 10, 6, _
				-1, -1, -1, -1}, {9, 2, 1, 9, 11, 2, _
				9, 4, 11, 7, 11, 4, _
				5, 10, 6, -1}, {8, 4, 7, 3, 11, 5, _
				3, 5, 1, 5, 11, 6, _
				-1, -1, -1, -1}, {5, 1, 11, 5, 11, 6, _
				1, 0, 11, 7, 11, 4, _
				0, 4, 11, -1}, {0, 5, 9, 0, 6, 5, _
				0, 3, 6, 11, 6, 3, _
				8, 4, 7, -1}, {6, 5, 9, 6, 9, 11, _
				4, 7, 9, 7, 11, 9, _
				-1, -1, -1, -1}, _
				{10, 4, 9, 6, 4, 10, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {4, 10, 6, 4, 9, 10, _
				0, 8, 3, -1, -1, -1, _
				-1, -1, -1, -1}, {10, 0, 1, 10, 6, 0, _
				6, 4, 0, -1, -1, -1, _
				-1, -1, -1, -1}, {8, 3, 1, 8, 1, 6, _
				8, 6, 4, 6, 1, 10, _
				-1, -1, -1, -1}, {1, 4, 9, 1, 2, 4, _
				2, 6, 4, -1, -1, -1, _
				-1, -1, -1, -1}, {3, 0, 8, 1, 2, 9, _
				2, 4, 9, 2, 6, 4, _
				-1, -1, -1, -1}, _
				{0, 2, 4, 4, 2, 6, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {8, 3, 2, 8, 2, 4, _
				4, 2, 6, -1, -1, -1, _
				-1, -1, -1, -1}, {10, 4, 9, 10, 6, 4, _
				11, 2, 3, -1, -1, -1, _
				-1, -1, -1, -1}, {0, 8, 2, 2, 8, 11, _
				4, 9, 10, 4, 10, 6, _
				-1, -1, -1, -1}, {3, 11, 2, 0, 1, 6, _
				0, 6, 4, 6, 1, 10, _
				-1, -1, -1, -1}, {6, 4, 1, 6, 1, 10, _
				4, 8, 1, 2, 1, 11, _
				8, 11, 1, -1}, _
				{9, 6, 4, 9, 3, 6, _
				9, 1, 3, 11, 6, 3, _
				-1, -1, -1, -1}, {8, 11, 1, 8, 1, 0, _
				11, 6, 1, 9, 1, 4, _
				6, 4, 1, -1}, {3, 11, 6, 3, 6, 0, _
				0, 6, 4, -1, -1, -1, _
				-1, -1, -1, -1}, {6, 4, 8, 11, 6, 8, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {7, 10, 6, 7, 8, 10, _
				8, 9, 10, -1, -1, -1, _
				-1, -1, -1, -1}, {0, 7, 3, 0, 10, 7, _
				0, 9, 10, 6, 7, 10, _
				-1, -1, -1, -1}, _
				{10, 6, 7, 1, 10, 7, _
				1, 7, 8, 1, 8, 0, _
				-1, -1, -1, -1}, {10, 6, 7, 10, 7, 1, _
				1, 7, 3, -1, -1, -1, _
				-1, -1, -1, -1}, {1, 2, 6, 1, 6, 8, _
				1, 8, 9, 8, 6, 7, _
				-1, -1, -1, -1}, {2, 6, 9, 2, 9, 1, _
				6, 7, 9, 0, 9, 3, _
				7, 3, 9, -1}, {7, 8, 0, 7, 0, 6, _
				6, 0, 2, -1, -1, -1, _
				-1, -1, -1, -1}, {7, 3, 2, 6, 7, 2, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, _
				{2, 3, 11, 10, 6, 8, _
				10, 8, 9, 8, 6, 7, _
				-1, -1, -1, -1}, {2, 0, 7, 2, 7, 11, _
				0, 9, 7, 6, 7, 10, _
				9, 10, 7, -1}, {1, 8, 0, 1, 7, 8, _
				1, 10, 7, 6, 7, 10, _
				2, 3, 11, -1}, {11, 2, 1, 11, 1, 7, _
				10, 6, 1, 6, 7, 1, _
				-1, -1, -1, -1}, {8, 9, 6, 8, 6, 7, _
				9, 1, 6, 11, 6, 3, _
				1, 3, 6, -1}, {0, 9, 1, 11, 6, 7, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, _
				{7, 8, 0, 7, 0, 6, _
				3, 11, 0, 11, 6, 0, _
				-1, -1, -1, -1}, {7, 11, 6, -1, -1, -1, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {7, 6, 11, -1, -1, -1, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {3, 0, 8, 11, 7, 6, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {0, 1, 9, 11, 7, 6, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {8, 1, 9, 8, 3, 1, _
				11, 7, 6, -1, -1, -1, _
				-1, -1, -1, -1}, _
				{10, 1, 2, 6, 11, 7, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {1, 2, 10, 3, 0, 8, _
				6, 11, 7, -1, -1, -1, _
				-1, -1, -1, -1}, {2, 9, 0, 2, 10, 9, _
				6, 11, 7, -1, -1, -1, _
				-1, -1, -1, -1}, {6, 11, 7, 2, 10, 3, _
				10, 8, 3, 10, 9, 8, _
				-1, -1, -1, -1}, {7, 2, 3, 6, 2, 7, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {7, 0, 8, 7, 6, 0, _
				6, 2, 0, -1, -1, -1, _
				-1, -1, -1, -1}, _
				{2, 7, 6, 2, 3, 7, _
				0, 1, 9, -1, -1, -1, _
				-1, -1, -1, -1}, {1, 6, 2, 1, 8, 6, _
				1, 9, 8, 8, 7, 6, _
				-1, -1, -1, -1}, {10, 7, 6, 10, 1, 7, _
				1, 3, 7, -1, -1, -1, _
				-1, -1, -1, -1}, {10, 7, 6, 1, 7, 10, _
				1, 8, 7, 1, 0, 8, _
				-1, -1, -1, -1}, {0, 3, 7, 0, 7, 10, _
				0, 10, 9, 6, 10, 7, _
				-1, -1, -1, -1}, {7, 6, 10, 7, 10, 8, _
				8, 10, 9, -1, -1, -1, _
				-1, -1, -1, -1}, _
				{6, 8, 4, 11, 8, 6, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {3, 6, 11, 3, 0, 6, _
				0, 4, 6, -1, -1, -1, _
				-1, -1, -1, -1}, {8, 6, 11, 8, 4, 6, _
				9, 0, 1, -1, -1, -1, _
				-1, -1, -1, -1}, {9, 4, 6, 9, 6, 3, _
				9, 3, 1, 11, 3, 6, _
				-1, -1, -1, -1}, {6, 8, 4, 6, 11, 8, _
				2, 10, 1, -1, -1, -1, _
				-1, -1, -1, -1}, {1, 2, 10, 3, 0, 11, _
				0, 6, 11, 0, 4, 6, _
				-1, -1, -1, -1}, _
				{4, 11, 8, 4, 6, 11, _
				0, 2, 9, 2, 10, 9, _
				-1, -1, -1, -1}, {10, 9, 3, 10, 3, 2, _
				9, 4, 3, 11, 3, 6, _
				4, 6, 3, -1}, {8, 2, 3, 8, 4, 2, _
				4, 6, 2, -1, -1, -1, _
				-1, -1, -1, -1}, {0, 4, 2, 4, 6, 2, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {1, 9, 0, 2, 3, 4, _
				2, 4, 6, 4, 3, 8, _
				-1, -1, -1, -1}, {1, 9, 4, 1, 4, 2, _
				2, 4, 6, -1, -1, -1, _
				-1, -1, -1, -1}, _
				{8, 1, 3, 8, 6, 1, _
				8, 4, 6, 6, 10, 1, _
				-1, -1, -1, -1}, {10, 1, 0, 10, 0, 6, _
				6, 0, 4, -1, -1, -1, _
				-1, -1, -1, -1}, {4, 6, 3, 4, 3, 8, _
				6, 10, 3, 0, 3, 9, _
				10, 9, 3, -1}, {10, 9, 4, 6, 10, 4, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {4, 9, 5, 7, 6, 11, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {0, 8, 3, 4, 9, 5, _
				11, 7, 6, -1, -1, -1, _
				-1, -1, -1, -1}, _
				{5, 0, 1, 5, 4, 0, _
				7, 6, 11, -1, -1, -1, _
				-1, -1, -1, -1}, {11, 7, 6, 8, 3, 4, _
				3, 5, 4, 3, 1, 5, _
				-1, -1, -1, -1}, {9, 5, 4, 10, 1, 2, _
				7, 6, 11, -1, -1, -1, _
				-1, -1, -1, -1}, {6, 11, 7, 1, 2, 10, _
				0, 8, 3, 4, 9, 5, _
				-1, -1, -1, -1}, {7, 6, 11, 5, 4, 10, _
				4, 2, 10, 4, 0, 2, _
				-1, -1, -1, -1}, {3, 4, 8, 3, 5, 4, _
				3, 2, 5, 10, 5, 2, _
				11, 7, 6, -1}, _
				{7, 2, 3, 7, 6, 2, _
				5, 4, 9, -1, -1, -1, _
				-1, -1, -1, -1}, {9, 5, 4, 0, 8, 6, _
				0, 6, 2, 6, 8, 7, _
				-1, -1, -1, -1}, {3, 6, 2, 3, 7, 6, _
				1, 5, 0, 5, 4, 0, _
				-1, -1, -1, -1}, {6, 2, 8, 6, 8, 7, _
				2, 1, 8, 4, 8, 5, _
				1, 5, 8, -1}, {9, 5, 4, 10, 1, 6, _
				1, 7, 6, 1, 3, 7, _
				-1, -1, -1, -1}, {1, 6, 10, 1, 7, 6, _
				1, 0, 7, 8, 7, 0, _
				9, 5, 4, -1}, _
				{4, 0, 10, 4, 10, 5, _
				0, 3, 10, 6, 10, 7, _
				3, 7, 10, -1}, {7, 6, 10, 7, 10, 8, _
				5, 4, 10, 4, 8, 10, _
				-1, -1, -1, -1}, {6, 9, 5, 6, 11, 9, _
				11, 8, 9, -1, -1, -1, _
				-1, -1, -1, -1}, {3, 6, 11, 0, 6, 3, _
				0, 5, 6, 0, 9, 5, _
				-1, -1, -1, -1}, {0, 11, 8, 0, 5, 11, _
				0, 1, 5, 5, 6, 11, _
				-1, -1, -1, -1}, {6, 11, 3, 6, 3, 5, _
				5, 3, 1, -1, -1, -1, _
				-1, -1, -1, -1}, _
				{1, 2, 10, 9, 5, 11, _
				9, 11, 8, 11, 5, 6, _
				-1, -1, -1, -1}, {0, 11, 3, 0, 6, 11, _
				0, 9, 6, 5, 6, 9, _
				1, 2, 10, -1}, {11, 8, 5, 11, 5, 6, _
				8, 0, 5, 10, 5, 2, _
				0, 2, 5, -1}, {6, 11, 3, 6, 3, 5, _
				2, 10, 3, 10, 5, 3, _
				-1, -1, -1, -1}, {5, 8, 9, 5, 2, 8, _
				5, 6, 2, 3, 8, 2, _
				-1, -1, -1, -1}, {9, 5, 6, 9, 6, 0, _
				0, 6, 2, -1, -1, -1, _
				-1, -1, -1, -1}, _
				{1, 5, 8, 1, 8, 0, _
				5, 6, 8, 3, 8, 2, _
				6, 2, 8, -1}, {1, 5, 6, 2, 1, 6, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {1, 3, 6, 1, 6, 10, _
				3, 8, 6, 5, 6, 9, _
				8, 9, 6, -1}, {10, 1, 0, 10, 0, 6, _
				9, 5, 0, 5, 6, 0, _
				-1, -1, -1, -1}, {0, 3, 8, 5, 6, 10, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {10, 5, 6, -1, -1, -1, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, _
				{11, 5, 10, 7, 5, 11, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {11, 5, 10, 11, 7, 5, _
				8, 3, 0, -1, -1, -1, _
				-1, -1, -1, -1}, {5, 11, 7, 5, 10, 11, _
				1, 9, 0, -1, -1, -1, _
				-1, -1, -1, -1}, {10, 7, 5, 10, 11, 7, _
				9, 8, 1, 8, 3, 1, _
				-1, -1, -1, -1}, {11, 1, 2, 11, 7, 1, _
				7, 5, 1, -1, -1, -1, _
				-1, -1, -1, -1}, {0, 8, 3, 1, 2, 7, _
				1, 7, 5, 7, 2, 11, _
				-1, -1, -1, -1}, _
				{9, 7, 5, 9, 2, 7, _
				9, 0, 2, 2, 11, 7, _
				-1, -1, -1, -1}, {7, 5, 2, 7, 2, 11, _
				5, 9, 2, 3, 2, 8, _
				9, 8, 2, -1}, {2, 5, 10, 2, 3, 5, _
				3, 7, 5, -1, -1, -1, _
				-1, -1, -1, -1}, {8, 2, 0, 8, 5, 2, _
				8, 7, 5, 10, 2, 5, _
				-1, -1, -1, -1}, {9, 0, 1, 5, 10, 3, _
				5, 3, 7, 3, 10, 2, _
				-1, -1, -1, -1}, {9, 8, 2, 9, 2, 1, _
				8, 7, 2, 10, 2, 5, _
				7, 5, 2, -1}, _
				{1, 3, 5, 3, 7, 5, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {0, 8, 7, 0, 7, 1, _
				1, 7, 5, -1, -1, -1, _
				-1, -1, -1, -1}, {9, 0, 3, 9, 3, 5, _
				5, 3, 7, -1, -1, -1, _
				-1, -1, -1, -1}, {9, 8, 7, 5, 9, 7, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {5, 8, 4, 5, 10, 8, _
				10, 11, 8, -1, -1, -1, _
				-1, -1, -1, -1}, {5, 0, 4, 5, 11, 0, _
				5, 10, 11, 11, 3, 0, _
				-1, -1, -1, -1}, _
				{0, 1, 9, 8, 4, 10, _
				8, 10, 11, 10, 4, 5, _
				-1, -1, -1, -1}, {10, 11, 4, 10, 4, 5, _
				11, 3, 4, 9, 4, 1, _
				3, 1, 4, -1}, {2, 5, 1, 2, 8, 5, _
				2, 11, 8, 4, 5, 8, _
				-1, -1, -1, -1}, {0, 4, 11, 0, 11, 3, _
				4, 5, 11, 2, 11, 1, _
				5, 1, 11, -1}, {0, 2, 5, 0, 5, 9, _
				2, 11, 5, 4, 5, 8, _
				11, 8, 5, -1}, {9, 4, 5, 2, 11, 3, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, _
				{2, 5, 10, 3, 5, 2, _
				3, 4, 5, 3, 8, 4, _
				-1, -1, -1, -1}, {5, 10, 2, 5, 2, 4, _
				4, 2, 0, -1, -1, -1, _
				-1, -1, -1, -1}, {3, 10, 2, 3, 5, 10, _
				3, 8, 5, 4, 5, 8, _
				0, 1, 9, -1}, {5, 10, 2, 5, 2, 4, _
				1, 9, 2, 9, 4, 2, _
				-1, -1, -1, -1}, {8, 4, 5, 8, 5, 3, _
				3, 5, 1, -1, -1, -1, _
				-1, -1, -1, -1}, {0, 4, 5, 1, 0, 5, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, _
				{8, 4, 5, 8, 5, 3, _
				9, 0, 5, 0, 3, 5, _
				-1, -1, -1, -1}, {9, 4, 5, -1, -1, -1, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {4, 11, 7, 4, 9, 11, _
				9, 10, 11, -1, -1, -1, _
				-1, -1, -1, -1}, {0, 8, 3, 4, 9, 7, _
				9, 11, 7, 9, 10, 11, _
				-1, -1, -1, -1}, {1, 10, 11, 1, 11, 4, _
				1, 4, 0, 7, 4, 11, _
				-1, -1, -1, -1}, {3, 1, 4, 3, 4, 8, _
				1, 10, 4, 7, 4, 11, _
				10, 11, 4, -1}, _
				{4, 11, 7, 9, 11, 4, _
				9, 2, 11, 9, 1, 2, _
				-1, -1, -1, -1}, {9, 7, 4, 9, 11, 7, _
				9, 1, 11, 2, 11, 1, _
				0, 8, 3, -1}, {11, 7, 4, 11, 4, 2, _
				2, 4, 0, -1, -1, -1, _
				-1, -1, -1, -1}, {11, 7, 4, 11, 4, 2, _
				8, 3, 4, 3, 2, 4, _
				-1, -1, -1, -1}, {2, 9, 10, 2, 7, 9, _
				2, 3, 7, 7, 4, 9, _
				-1, -1, -1, -1}, {9, 10, 7, 9, 7, 4, _
				10, 2, 7, 8, 7, 0, _
				2, 0, 7, -1}, _
				{3, 7, 10, 3, 10, 2, _
				7, 4, 10, 1, 10, 0, _
				4, 0, 10, -1}, {1, 10, 2, 8, 7, 4, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {4, 9, 1, 4, 1, 7, _
				7, 1, 3, -1, -1, -1, _
				-1, -1, -1, -1}, {4, 9, 1, 4, 1, 7, _
				0, 8, 1, 8, 7, 1, _
				-1, -1, -1, -1}, {4, 0, 3, 7, 4, 3, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {4, 8, 7, -1, -1, -1, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, _
				{9, 10, 8, 10, 11, 8, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {3, 0, 9, 3, 9, 11, _
				11, 9, 10, -1, -1, -1, _
				-1, -1, -1, -1}, {0, 1, 10, 0, 10, 8, _
				8, 10, 11, -1, -1, -1, _
				-1, -1, -1, -1}, {3, 1, 10, 11, 3, 10, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {1, 2, 11, 1, 11, 9, _
				9, 11, 8, -1, -1, -1, _
				-1, -1, -1, -1}, {3, 0, 9, 3, 9, 11, _
				1, 2, 9, 2, 11, 9, _
				-1, -1, -1, -1}, _
				{0, 2, 11, 8, 0, 11, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {3, 2, 11, -1, -1, -1, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {2, 3, 8, 2, 8, 10, _
				10, 8, 9, -1, -1, -1, _
				-1, -1, -1, -1}, {9, 10, 2, 0, 9, 2, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {2, 3, 8, 2, 8, 10, _
				0, 1, 8, 1, 10, 8, _
				-1, -1, -1, -1}, {1, 10, 2, -1, -1, -1, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, _
				{1, 3, 8, 9, 1, 8, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {0, 9, 1, -1, -1, -1, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {0, 3, 8, -1, -1, -1, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1, -1, -1, _
				-1, -1, -1, -1}}

			'
'      Determine the index into the edge table which
'      tells us which vertices are inside of the surface
'   

			cubeindex = 0
			If grid.val(0) < isolevel Then
				cubeindex = cubeindex Or 1
			End If
			If grid.val(1) < isolevel Then
				cubeindex = cubeindex Or 2
			End If
			If grid.val(2) < isolevel Then
				cubeindex = cubeindex Or 4
			End If
			If grid.val(3) < isolevel Then
				cubeindex = cubeindex Or 8
			End If
			If grid.val(4) < isolevel Then
				cubeindex = cubeindex Or 16
			End If
			If grid.val(5) < isolevel Then
				cubeindex = cubeindex Or 32
			End If
			If grid.val(6) < isolevel Then
				cubeindex = cubeindex Or 64
			End If
			If grid.val(7) < isolevel Then
				cubeindex = cubeindex Or 128
			End If

			' Cube is entirely in/out of the surface 

			If edgeTable(cubeindex) = 0 Then
				Return (0)
			End If

			Dim ct As New ClassMTeth()
			' Find the vertices where the surface intersects the cube 

			If (edgeTable(cubeindex) And 1) = 1 Then
				vertlist(0) = ct.VertexInterp(isolevel, grid.p(0), grid.p(1), grid.val(0), grid.val(1))
			End If
			If (edgeTable(cubeindex) And 2) = 2 Then
				vertlist(1) = ct.VertexInterp(isolevel, grid.p(1), grid.p(2), grid.val(1), grid.val(2))
			End If
			If (edgeTable(cubeindex) And 4) = 4 Then
				vertlist(2) = ct.VertexInterp(isolevel, grid.p(2), grid.p(3), grid.val(2), grid.val(3))
			End If
			If (edgeTable(cubeindex) And 8) = 8 Then
				vertlist(3) = ct.VertexInterp(isolevel, grid.p(3), grid.p(0), grid.val(3), grid.val(0))
			End If
			If (edgeTable(cubeindex) And 16) = 16 Then
				vertlist(4) = ct.VertexInterp(isolevel, grid.p(4), grid.p(5), grid.val(4), grid.val(5))
			End If
			If (edgeTable(cubeindex) And 32) = 32 Then
				vertlist(5) = ct.VertexInterp(isolevel, grid.p(5), grid.p(6), grid.val(5), grid.val(6))
			End If
			If (edgeTable(cubeindex) And 64) = 64 Then
				vertlist(6) = ct.VertexInterp(isolevel, grid.p(6), grid.p(7), grid.val(6), grid.val(7))
			End If
			If (edgeTable(cubeindex) And 128) = 128 Then
				vertlist(7) = ct.VertexInterp(isolevel, grid.p(7), grid.p(4), grid.val(7), grid.val(4))
			End If
			If (edgeTable(cubeindex) And 256) = 256 Then
				vertlist(8) = ct.VertexInterp(isolevel, grid.p(0), grid.p(4), grid.val(0), grid.val(4))
			End If
			If (edgeTable(cubeindex) And 512) = 512 Then
				vertlist(9) = ct.VertexInterp(isolevel, grid.p(1), grid.p(5), grid.val(1), grid.val(5))
			End If
			If (edgeTable(cubeindex) And 1024) = 1024 Then
				vertlist(10) = ct.VertexInterp(isolevel, grid.p(2), grid.p(6), grid.val(2), grid.val(6))
			End If
			If (edgeTable(cubeindex) And 2048) = 2048 Then
				vertlist(11) = ct.VertexInterp(isolevel, grid.p(3), grid.p(7), grid.val(3), grid.val(7))
			End If

			' Create the triangle 

			ntriang = 0
			i = 0
			While triTable(cubeindex, i) <> -1
				triangles(ntriang).p(0) = vertlist(triTable(cubeindex, i))
				triangles(ntriang).p(1) = vertlist(triTable(cubeindex, i + 1))
				triangles(ntriang).p(2) = vertlist(triTable(cubeindex, i + 2))
				ntriang += 1
				i += 3
			End While

			Return (ntriang)
		End Function
	End Class

